package com.smartlifev30.mvp.presenter;

import android.content.Context;
import android.net.ConnectivityManager;
import android.text.TextUtils;

import com.smartlifev30.Constants;
import com.smartlifev30.R;
import com.smartlifev30.baiwei.App;
import com.smartlifev30.mvp.module.IPrepareActivityModel;
import com.smartlifev30.mvp.view.IPrepareActivityView;
import com.smartlifev30.network.BaseCallBack;
import com.smartlifev30.utils.ApkUtils;
import com.smartlifev30.utils.NetUtils;

import smartlifev30.com.sdk.StatusCode;
import smartlifev30.com.sdk.bean.response.LoginResponse;

/**
 * @author tys
 * @date 2018/8/29
 */

public class PrepareActivityPresent extends BasePresenter<IPrepareActivityView, IPrepareActivityModel> {
    public PrepareActivityPresent(IPrepareActivityView view, IPrepareActivityModel model) {
        super(view, model);
    }

    /**
     * 根据versionCode选择是进入向导页面还是登陆页面
     *
     * @param context
     */
    public void selectedActivity(Context context) {
        int currentVersionCode = ApkUtils.getAppVersionCode(context);
        int lastVersionCode = model.getLastVersionCode(context);
        IPrepareActivityView prepareActivityView = view.get();
        if (prepareActivityView != null) {
            if (currentVersionCode > lastVersionCode) {
                //更新后或者安裝
                model.setLastVersionCode(context, currentVersionCode);
                prepareActivityView.toGuideActivity();
            } else {
                String token = model.getServerToken(context);
                String userName = model.getUserName(context);
                if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(userName)) {
                    //有上次登陆记录 token登录
                    model.loginServerByToken(userName, token, new BaseCallBack<LoginResponse>() {
                        @Override
                        protected void onSuccess(LoginResponse response) {
                            model.saveLoinInfoByToken(userName, token);
                            prepareActivityView.toGatewayActivity("正常登录", null);
                        }

                        @Override
                        protected void onTimeOut(String detailMessage) {
                            //有记录登录超时 ==>
                            int currentNetStatus = NetUtils.getCurrentNetStatus(App.getInstance());
                            if (ConnectivityManager.TYPE_WIFI == currentNetStatus) {
                                //wifi 局域网模式
                                prepareActivityView.toast(R.string.remote_login_failed);
                                prepareActivityView.toGatewayActivity("wifi登录超时", Constants.LOGIN_TIME_OUT);
                            } else if (ConnectivityManager.TYPE_MOBILE == currentNetStatus) {
                                // 4g 登录界面
                                prepareActivityView.toast(R.string.login_timeout);
                                prepareActivityView.toLoginActivity("流量登录超时", null);
                            } else if (-1 == currentNetStatus) {
                                prepareActivityView.toast(R.string.net_connect_error);
                                prepareActivityView.toLoginActivity("网络不可用", null);
                            } else {
                                super.onTimeOut(detailMessage);
                            }
                        }

                        @Override
                        protected void onFailed(Throwable throwable, int code) {
                            switch (code) {
                                case StatusCode.TOKEN_OUT_TIME:
                                    prepareActivityView.toast(R.string.token_out_login);
                                    prepareActivityView.toLoginActivity("token过期", StatusCode.TOKEN_OUT_TIME);
                                    break;

                                case StatusCode.TOKEN_NO_USE:
                                    prepareActivityView.toast(R.string.token_invalid_login);
                                    prepareActivityView.toLoginActivity("token无效", StatusCode.TOKEN_NO_USE);
                                    break;
                                default:
                                    super.onFailed(throwable, code);
                            }
                        }
                    });

                } else {
                    prepareActivityView.toLoginActivity("没有记录", null);
                }
            }
        }
    }

}
